package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/*
 2. 获取构造方法们
   - Constructor<?>[] getConstructors()
   - Constructor<T> getConstructor(类<?>... parameterTypes)
   - Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
   - Constructor<?>[] getDeclaredConstructors()
 */
public class ReflectConstructors {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class personClass = Person.class;

        //Constructor<?>[] getConstructors()
        Constructor constructor = personClass.getConstructor(String.class, int.class);
        System.out.println("constructor = [" + constructor + "]");

        //创建对象
        Object kaka = constructor.newInstance("kaka", 22);
        System.out.println("person="+kaka);

        //空参构造
        Constructor constructor1 = personClass.getConstructor();
        System.out.println("constructor1 = [" + constructor1 + "]");
        Object kaka1 = constructor1.newInstance();
        System.out.println("person="+kaka1);

        //简化空参构造
        Object kaka3=personClass.newInstance();
        System.out.println("person = [" + kaka3 + "]");
    }
}
